package day20171122;

import java.sql.*;

public class TestMysql {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");    //加载驱动程序
                                                                      //ip地址        数据库名称
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java_2017?serverTimezone=UTC", "root", "193749");  //与数据库建立连接第一个参数是数据链接字符串
            //第二个参数是用户名，第三个参数是密码
            statement = connection.createStatement();                       //链接数据库的对象connection，调用createstatement方法创建sql语句的对象
            ResultSet resultSet = statement.executeQuery("SELECT * FROM  t_students");      //将查询结果放在这个集合中
            while (resultSet.next()) {                                               //遍历set集合，输出查询结果，只有在查的操作才用到此操作。
                System.out.print(resultSet.getString(1) + " ");
                System.out.print(resultSet.getString(2) + " ");
                System.out.print(resultSet.getString(3) + " ");
                System.out.print(resultSet.getString(4) + " ");
                System.out.println(resultSet.getString(5));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                statement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
